//
//  SampleBookViewFlooring.m
//  Zinipad
//
//  Created by ZeLkOvA on 12. 5. 23..
//  Copyright (c) 2012년 __MyCompanyName__. All rights reserved.
//

#import "SampleBookViewFlooring.h"
#import "iCarousel.h"
#import "Util.h"
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)

#define NUMBER_OF_ITEMS 6
#define NUMBER_OF_VISIBLE_ITEMS (IS_IPAD? 5: 19)
#define ITEM_SPACING 320.0f
#define INCLUDE_PLACEHOLDERS YES

#define SIZE_TOPBAR 60.0f

@interface SampleBookViewFlooring ()<UIActionSheetDelegate>
@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, retain) NSMutableArray *items;
@end

@implementation SampleBookViewFlooring

@synthesize carousel;
@synthesize wrap;
@synthesize items;
@synthesize imgArray;
@synthesize floorList;

- (void)setUpImg
{
    //set up data
    wrap = YES;
    self.items = [NSMutableArray array];
    imgArray = [[NSMutableArray alloc] init];
    
    // 이미지 가져와서 저장하기 
   
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
   
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    
   
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    [imgArray addObject:@"x.png"];
    [imgArray addObject:@"y.png"];
    [imgArray addObject:@"z.png"];
    
    //    }
    self.items = imgArray;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setUpImg];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    [self InitCarousel];
    [self InitFloorList];
	// Do any additional setup after loading the view.
}

-(void)InitCarousel
{
    NSLog(@"HOHOHOHO");
    //    count = 0;
    carousel = [[iCarousel alloc] init];
    [carousel setFrame:CGRectMake(0,0,1024-188-188/2,600-SIZE_TOPBAR)];
    [self.view addSubview:carousel];
    carousel.delegate = self;
    carousel.dataSource = self;
    //configure carousel
    carousel.type = iCarouselTypeCoverFlow;
    //    navItem.title = @"CoverFlow2";
//    carousel.vertical = !carousel.vertical;
    
}
-(void)InitFloorList
{
    //    carouselColor
    floorList = [[iCarouselColor alloc] init];
    [floorList setFrame:CGRectMake(80,500,600,200)];
    floorList.dataSourceColor = self;
    floorList.delegate = self;
    [self.view addSubview:floorList];
    //    carouselColor.delegate = self;
    //    carouselColor.dataSourceColor = self;
    floorList.type = iCarouselFloorTypeLinear;
//    floorList.vertical = !floorList.vertical;
    //    ColorView = [[SampleBookColorViewWallpaper alloc] init];
    //    [ColorView.view setFrame:CGRectMake(40,100,150,300)];
    //    [self.view addSubview:ColorView.view];
    
}
- (void)carouselCurrnetImgIndex:(int)_imgIndex;
{
    NSLog(@"ImgIndex == %d",_imgIndex);
//    [floorList scrollToItemAtIndex:_imgIndex animated:YES];
//    UIImage* tileBGImage = [UIImage imageNamed:[imgArray objectAtIndex:_imgIndex]];
//    [detailImageView setImage:tileBGImage];
    //    detailImageView = [[UIImageView alloc] initWithImage:tileBGImage];
    //    [detailImageView setFrame:CGRectMake(50, 0, 300, 400)];
}
- (void)carouselCurrnetImgIndexColor:(int)_imgIndex;
{
    NSLog(@"ImgIndexColor    == %d",_imgIndex);
   [carousel scrollToItemAtIndex:_imgIndex animated:YES];
    //    if(bFirstLoad)
    //    {
    //        bFirstLoad = NO;
    //        return;
    //    }
    //    if(carousel)
    //    {
    //        [carousel removeFromSuperview];
    //        [carousel release];
    //    }
    
    //    UIImage* tileBGImage = [UIImage imageNamed:[items objectAtIndex:_imgIndex]];
    //    [detailImageView setImage:tileBGImage];
    //    [self setUpImg];
    //    [self InitCarousel];
    
    //    [carousel scrollToItemAtIndex:_imgIndex animated:YES];
    
}

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return [items count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return NUMBER_OF_VISIBLE_ITEMS;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (view == nil)
    {
        //         NSLog(@"here %d",count);
//        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[imgArray objectAtIndex:index]]] autorelease];
        view = [[UIImageView alloc] init];
        [view setFrame:CGRectMake(0, 0, 480, 400)];
        UIImage* aaaa = [UIImage imageNamed:@"img_shadow.png"];
        UIImageView* aaaaaaaaaaa = [[UIImageView alloc] initWithImage:aaaa];
        [aaaaaaaaaaa setFrame:CGRectMake(0, 0, 480, 400)];
        UIImageView* reflect = [Util cropImage:[imgArray objectAtIndex:index] targetX:0 targetY:250 targetWidth:480-27*2 targetHeight:50 resultX:25 resultY:400-40 resultWidth:480-27*2 resultHeight:50];
        
        reflect.transform = CGAffineTransformMakeRotation(1.57*2);//Right -1.57 Left
        [reflect setAlpha:0.3f];
        [view addSubview:reflect];
        [view addSubview:aaaaaaaaaaa];
        
        UIImage* bbbb = [UIImage imageNamed:[imgArray objectAtIndex:index]];
        UIImageView* bbbbbb = [[UIImageView alloc] initWithImage:bbbb];
        [bbbbbb setFrame:CGRectMake(25, 21, 480-27*2, 400-21*2)];
        [aaaaaaaaaaa addSubview:bbbbbb];
        
       
//        [arView setHidden:NO];
        
//        view.layer.doubleSided = NO; //prevent back side of view from showing
//        UIImage* aaaa = [UIImage imageNamed:@"page.png"];
//        UIImageView* aaaaaaaaaaa = [[UIImageView alloc] initWithImage:aaaa];
//        [aaaaaaaaaaa setFrame:CGRectMake(0, 300, 400, 150)];
//        [view addSubview:aaaaaaaaaaa];
        
    // 라벨 들어갈자리    /////////////////
//        UILabel* cccc = 
    ///////////////////////
        
        
        //       count++;
    }
    else
    {
        view = [[UIImageView alloc] init];
        [view setFrame:CGRectMake(0, 0, 480, 400)];
        UIImage* aaaa = [UIImage imageNamed:@"img_shadow.png"];
        UIImageView* aaaaaaaaaaa = [[UIImageView alloc] initWithImage:aaaa];
        [aaaaaaaaaaa setFrame:CGRectMake(0, 0, 480, 400)];
        UIImageView* reflect = [Util cropImage:[imgArray objectAtIndex:index] targetX:0 targetY:250 targetWidth:480-27*2 targetHeight:50 resultX:25 resultY:400-40 resultWidth:480-27*2 resultHeight:50];
        
        reflect.transform = CGAffineTransformMakeRotation(1.57*2);//Right -1.57 Left
        [reflect setAlpha:0.3f];
        [view addSubview:reflect];
        [view addSubview:aaaaaaaaaaa];
        
        UIImage* bbbb = [UIImage imageNamed:[imgArray objectAtIndex:index]];
        UIImageView* bbbbbb = [[UIImageView alloc] initWithImage:bbbb];
        [bbbbbb setFrame:CGRectMake(25, 21, 480-27*2, 400-21*2)];
        [aaaaaaaaaaa addSubview:bbbbbb];

    }
    
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return INCLUDE_PLACEHOLDERS? 2: 0;
}



- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    //usually this should be slightly wider than the item views
    return ITEM_SPACING;
}

//- (CGFloat)carousel:(iCarousel *)carousel itemAlphaForOffset:(CGFloat)offset
//{
//    //set opacity based on distance from camera
//    return 1.0f - fminf(fmaxf(offset, 0.0f), 1.0f);
//}

//- (CATransform3D)carousel:(iCarousel *)_carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
//{
//    //implement 'flip3D' style carousel
//    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
//    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * carousel.itemWidth);
//}
-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"%d",index);
    
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return wrap;
}

-(void)dealloc
{
    carousel.delegate = nil;
    carousel.dataSource = nil;
    [carousel release];
    [items release];
    [super dealloc];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (NSUInteger)numberOfItemsInCarouselColor:(iCarouselColor *)carousel
{
    return [items count];
}

- (NSUInteger)numberOfVisibleItemsInCarouselColor:(iCarouselColor *)carousel
{
    //limit the number of items views loaded concurrently (for performance reasons)
    //this also affects the appearance of circular-type carousels
    return 13;
}

- (UIView *)carouselColor:(iCarouselColor *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    if (view == nil)
    {
        //        NSLog(@"here %d",index);
        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]] autorelease];
        [view setFrame:CGRectMake(0, 0, 70, 70)];
        view.layer.doubleSided = YES; //prevent back side of view from showing
        
        //        label = [[[UILabel alloc] initWithFrame:view.bounds] autorelease];
        //        label.backgroundColor = [UIColor clearColor];
        //        label.textAlignment = UITextAlignmentCenter;
        //        label.font = [label.font fontWithSize:10];
        //        label.text =  [NSString stringWithFormat:@"%d",count];
        //        [view addSubview:label];
        //        count++;
    }
    else
    {
        //        NSLog(@"here %d",index);
        view = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[items objectAtIndex:index]]] autorelease];
        [view setFrame:CGRectMake(0, 0, 70, 70)];
        view.layer.doubleSided = YES; 
        
    }
    //   
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarouselColor:(iCarouselColor *)carousel
{
    //note: placeholder views are only displayed on some carousels if wrapping is disabled
    return 0;
}


- (CGFloat)carouselItemWidthColor:(iCarouselColor *)carousel
{
    //usually this should be slightly wider than the item views
    return 100;
}

- (BOOL)carouselShouldWrapColor:(iCarouselColor *)carousel
{
    return wrap;
}


@end
